//
//  TongjiCell.swift
//  DiZhenYuJing
//
//  Created by zhibao on 2024/11/1.
//

import UIKit

@objc class TongjiCell: UITableViewCell {
    var bgView:UIView!
    var rightIcon:UIImageView!
    var nameLabel:UILabel!
    var timeLabel:UILabel!
    var jingweiLabel:UILabel!
    var shendiLabel:UILabel!
    var dengjiLabel:UILabel!
    var juliLabel:UILabel!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = UIColor.clear
        
        bgView = UIView()
        bgView.backgroundColor = MYColor.color(withHexString: "ffffff", alpha: 0.95)
        self.contentView.addSubview(bgView)
        bgView.layer.cornerRadius = pixw(p:15)
        bgView.snp.makeConstraints { make in
            make.left.equalTo(pixw(p:15));
            make.top.equalTo(pixw(p:10));
            make.right.equalTo(pixw(p:-15));
            make.bottom.equalTo(0);
        }
        
        rightIcon = UIImageView(image: UIImage(named: "comm_right"))
        bgView.addSubview(rightIcon)
        rightIcon.snp.makeConstraints { make in
            make.top.equalTo(pixw(p: 15))
            //make.width.height.equalTo(pixw(p: 22))
            make.right.equalTo(bgView).offset(pixw(p: -15))
        }
        
        nameLabel = UILabel()
        nameLabel.text = "新疆阿克苏地区库车市"
        nameLabel.font = MY_SCALE_SemiboldFONT(14)
        nameLabel.textColor = MYBlock
        nameLabel.numberOfLines = 0
        nameLabel.lineBreakMode = .byWordWrapping
        bgView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.top.equalTo(pixw(p: 12))
            make.right.equalTo(rightIcon.snp.left).offset(pixw(p:-15))
        }
        
        timeLabel = UILabel()
        timeLabel.text = "地震时间:2024-10-15 21:15:25"
        timeLabel.font = MY_SCALE_RegularFONT(12)
        timeLabel.textColor = MYColor.color(withHexString: "#999999")
        bgView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel);
            make.top.equalTo(nameLabel.snp.bottom).offset(pixw(p:8));
        }
        
        jingweiLabel = UILabel()
        jingweiLabel.text = "经度:83.98 纬度:40.91"
        jingweiLabel.font = MY_SCALE_RegularFONT(12)
        jingweiLabel.textColor = MYColor.color(withHexString: "#999999")
        bgView.addSubview(jingweiLabel)
        jingweiLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel);
            make.top.equalTo(timeLabel.snp.bottom).offset(pixw(p:4));
        }
        
        shendiLabel = UILabel()
        shendiLabel.text = "深度:18千米"
        shendiLabel.font = MY_SCALE_RegularFONT(12)
        shendiLabel.textColor = MYColor.color(withHexString: "#999999")
        bgView.addSubview(shendiLabel)
        shendiLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel);
            make.top.equalTo(jingweiLabel.snp.bottom).offset(pixw(p:4))
            make.bottom.equalTo(bgView).offset(pixw(p: -10))
        }
        
        juliLabel = UILabel()
        juliLabel.text = "距你：0km"
        juliLabel.font = MY_SCALE_BoldFont(11)
        juliLabel.textColor = MYColor.color(withHexString: "#fbb621")
        bgView.addSubview(juliLabel)
        juliLabel.snp.makeConstraints { make in
            make.right.equalTo(rightIcon);
            make.bottom.equalTo(shendiLabel);
        }
        
        dengjiLabel = UILabel()
        dengjiLabel.text = "3.2级"
        dengjiLabel.font = MY_SCALE_BoldFont(16)
        dengjiLabel.textColor = MYRed
        bgView.addSubview(dengjiLabel)
        dengjiLabel.snp.makeConstraints { make in
            make.right.equalTo(rightIcon);
            make.bottom.equalTo(juliLabel.snp.top).offset(pixw(p: -4));
        }
        
        addbulrEffectView(superView: self.bgView)
    }
    
    func updata(dic:[String:Any]?){
        if dic == nil {
            return
        }
        nameLabel.text = dic!["location"] as? String ?? ""
        timeLabel.text = dic!["time"] as? String ?? ""
        jingweiLabel.text = "\(LocalizedString("经度")):\(dic!["lon"] ?? "0")°   \(LocalizedString("纬度")):\(dic!["lat"] ?? "0")°"
        shendiLabel.text = "\(LocalizedString("深度")):\(dic!["depth"] ?? "0")km"
        dengjiLabel.text = "\(dic!["mag"] ?? "")级"
        
        //地震经纬度
        let dzLat = Double(dic!["lat"] as! String) ?? 0.00
        let dzLon = Double(dic!["lon"] as! String) ?? 0.00
        //自己位置
        let zjLat = MYGlobal.share().locationModel.lat
        let zjLog = MYGlobal.share().locationModel.lon
        
//        let location1 = CLLocationCoordinate2D(latitude: dzLat, longitude: dzLon)
//        let location2 = CLLocationCoordinate2D(latitude: zjLat, longitude: zjLog)
    
        // 将CLLocationCoordinate2D结构转换为CLLocation对象
        let place1 = CLLocation(latitude: dzLat, longitude: dzLon)
        let place2 = CLLocation(latitude: zjLat, longitude: zjLog)
         
        // 使用distance(from:)方法计算两点间的距离
        let distance = place2.distance(from: place1) / 1000
        
        juliLabel.text = "\(LocalizedString("距您")):\(String(format: "%.2f", distance))km"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

